import  Logger  from '../common/logger';
import mongoose from 'mongoose';

import BackTestKlineSchema from "./BackTestKlineSchema";
import KlineSchema from "./KlineSchema";
import MyTradeSchema from "./MyTradeSchema";

import BackTestKlineModel from "./BackTestKlineModel";
import KlineModel from "./KlineModel";
import MyTradeModel from "./MyTradeModel";

import config from "../config";
import klineSchema from "./KlineSchema";

class Storage {
    connection:mongoose.Connection;
    public readonly logger:Logger;
    klineMap:Map<string, KlineModel>;
    backTestKlineMap:Map<string, BackTestKlineModel>;
    myTradeModel?:MyTradeModel;

    constructor(connection: mongoose.Connection,logger: Logger) {
        this.connection = connection;
        this.logger = logger;

        this.klineMap = new Map();
        let dklineModel =  new KlineModel(this.connection, config.storage.dklinesCol, KlineSchema, this.logger);
        let wklineModel =  new KlineModel(this.connection, config.storage.wklinesCol, KlineSchema, this.logger);
        let mklineModel =  new KlineModel(this.connection, config.storage.mklinesCol, KlineSchema, this.logger);
        let h6KlineModel = new KlineModel(this.connection, config.storage.h6KlinesCol, KlineSchema, this.logger);
        let h8KlineModel = new KlineModel(this.connection, config.storage.h8klinesCol, klineSchema, this.logger);
        let h12KlineModel = new KlineModel(this.connection, config.storage.h12klinesCol, KlineSchema, this.logger);
        this.klineMap.set('day', dklineModel);
        this.klineMap.set('week', wklineModel);
        this.klineMap.set('month', mklineModel);
        this.klineMap.set('h8kline', h8KlineModel);
        this.klineMap.set('h6kline', h6KlineModel);
        this.klineMap.set('h12kline', h12KlineModel);
        this.myTradeModel = new MyTradeModel(this.connection, config.storage.myTrades, MyTradeSchema, this.logger);

        this.backTestKlineMap = new Map();
        let dBackTestKlineModel =  new BackTestKlineModel(this.connection, 'backtest', BackTestKlineSchema, this.logger);
        this.backTestKlineMap.set('backtest', dBackTestKlineModel);

    }
    getKlineCol(type:string) {
        return this.klineMap.get(type);
    }
    getH6KlineCol() {
        return this.getKlineCol('h6Kline');
    }
    getH8KlineCol()   {
        return this.getKlineCol('h8kline');
    }
    getH12KlineCol() {
        return this.getKlineCol('h12kline')
    }
    getBacktestCol() {
        return this.getBackTestKlines('backtest');
    }
    getBackTestKlines(type:string) {
        return this.backTestKlineMap.get(type);
    }
    getMyTrade(){
        return this.myTradeModel;
    }
}

export default Storage;